package org.reliablesource.pleiades;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;

public class SelectedViewPart extends ViewPart implements ISelectionListener {

	public static final String ID = "org.reliablesource.pleiades.view.selected";
	private TableViewer viewer;

	private final List<ReliableSnippet> selectionList = new LinkedList<ReliableSnippet>();

	final static String COLS[] = { "Selected String", "URI", "Date\\Time",
			"Path" };

	final static int SELECTED_STR = 0;
	final static int URI = 1;
	final static int DATE = 2;
	final static int XPATH = 3;

	public SelectedViewPart() {
		super();
	}

	@Override
	public void createPartControl(Composite _parent) {

		viewer = new TableViewer(_parent);

		Table _tbl = viewer.getTable();
		TableColumn _col = null;
		_tbl.setHeaderVisible(true);
		_tbl.setLinesVisible(true);
		for (int i = 0; i < COLS.length; i++) {
			_col = new TableColumn(_tbl, SWT.LEFT);
			_col.setMoveable(true);
			_col.setResizable(true);
			_col.setText(COLS[i]);
			_col.pack();
		}

		_tbl.setLayoutDeferred(true);

		viewer.setContentProvider(new IStructuredContentProvider() {

			public void dispose() {
			}

			public void inputChanged(Viewer _viewer, Object _oldInput,
					Object _newInput) {
			}

			public Object[] getElements(Object _inputElement) {
				if (_inputElement instanceof List) {
					List<ReliableSnippet> _list = (List<ReliableSnippet>) _inputElement;
					return _list.toArray();
				}
				return new Object[0];
			}

		});

		class SemanticBindingLabelProvider implements ITableColorProvider,
				ITableLabelProvider {

			private final List<ILabelProviderListener> labelProviderListenerList = new LinkedList<ILabelProviderListener>();

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.ITableColorProvider#getBackground(java.lang.Object,
			 *      int)
			 */
			public Color getBackground(Object _element, int _columnIndex) {
				return null;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.ITableColorProvider#getForeground(java.lang.Object,
			 *      int)
			 */
			public Color getForeground(Object _element, int _columnIndex) {
				return null;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object,
			 *      int)
			 */
			public Image getColumnImage(Object _element, int _columnIndex) {
				return null;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object,
			 *      int)
			 */
			public String getColumnText(Object _element, int _columnIndex) {
				StringBuffer _retVal = new StringBuffer();

				if (!(_element instanceof ReliableSnippet)) {
					return _retVal.toString().trim();
				}

				ReliableSnippet _snip = (ReliableSnippet) _element;

				switch (_columnIndex) {
				case SELECTED_STR:
					_retVal.append(_snip.getRange().getContent());
					break;
				case URI:
					_retVal.append(_snip.getParent().getId());
					break;
				case XPATH:
					_retVal.append(_snip.getXPath());
					break;
				case DATE:
					_retVal.append(_snip.getDate());
					break;
				}
				return _retVal.toString().trim();
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
			 */
			public void addListener(final ILabelProviderListener _listener) {
				if (null != _listener)
					if (!labelProviderListenerList.contains(_listener))
						labelProviderListenerList.add(_listener);
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
			 */
			public void dispose() {
				labelProviderListenerList.clear();
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object,
			 *      java.lang.String)
			 */
			public boolean isLabelProperty(Object _element, String _property) {
				return false;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
			 */
			public void removeListener(ILabelProviderListener _listener) {
				if (null != _listener)
					if (labelProviderListenerList.contains(_listener))
						labelProviderListenerList.remove(_listener);
			}

		}

		viewer.setLabelProvider(new SemanticBindingLabelProvider());
		viewer.setInput(selectionList);
		getSite().setSelectionProvider(viewer);
		getSite().getWorkbenchWindow().getSelectionService()
				.addSelectionListener(this);

		getSite().registerContextMenu(new MenuManager(), viewer);

	}

	@Override
	public void setFocus() {
		if (viewer != null)
			if (viewer.getTable() != null)
				viewer.getTable().setFocus();

	}

	/**
	 * 
	 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
	 *      org.eclipse.jface.viewers.ISelection)
	 */
	public void selectionChanged(final IWorkbenchPart _part,
			final ISelection _selection) {

		if (!(_part instanceof BrowserView))
			return;

		if (_selection.equals(StructuredSelection.EMPTY))
			return;

		if (_selection instanceof IStructuredSelection) {
			ReliableSnippet _selected = (ReliableSnippet) ((IStructuredSelection) _selection)
					.getFirstElement();

			if (!selectionList.contains(_selected)) {
				selectionList.add(_selected);

				if (viewer != null)
					viewer.refresh();
			}
		}

	}

}
